﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using AffinityORM.BusinessLayer;
using AffinityORM.DataTypes;

namespace AffinityORM.BusinessLayer
{
    class UdlConnectionManager
    {
        private string _path;

        public UdlConnectionManager()
        {
            _path = ConfigurationManager.AppSettings["udlfilelocation"].ToString();
        }

        public void CreateUdlConnectionFile(string filename)
        {
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);
            FileStream fs = new FileStream(_path + filename + ".udl", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            fs.Close();
            System.Diagnostics.Process.Start(_path + filename + ".udl");
        }

        public List<UdlConnection> GetUdlConnections()
        {
            List<UdlConnection> connections = new List<UdlConnection>();

            if (Directory.Exists(_path))
            {
                DirectoryInfo directory = new DirectoryInfo(_path);
                FileInfo[] files = directory.GetFiles("*.udl");
                foreach (FileInfo file in files)
                {
                    UdlConnection conn = new UdlConnection();
                    conn.ConnectionStringSettings.Name = file.Name;
                    FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);

                    while (!sr.EndOfStream)
                    {
                        string result = sr.ReadLine();

                        try
                        {
                            if (result.Contains("Provider"))
                            {
                                conn.ConnectionStringSettings.ConnectionString = result;
                                int indexequal = result.IndexOf('=') + 1;
                                int indexsemi = result.IndexOf(';') - indexequal;
                                string providerName = result.Substring(indexequal, indexsemi);
                                if (providerName.ToUpper().Contains("OLEDB"))
                                    providerName = "System.Data.OleDb";
                                else if (providerName.ToUpper().Contains("SQLNCLI"))
                                    providerName = "System.Data.SqlClient";
                                else if (providerName.ToUpper().Contains("ODBC"))
                                    providerName = "System.Data.Odbc";
                                conn.ConnectionStringSettings.ProviderName = providerName;
                                connections.Add(conn);
                            }
                        }
                        catch { }
                    }

                    sr.Close();
                }
            }

            return connections;
        }
    }
}
